/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.FailureReport;
import com.zhuoyue.elmt.entity.FailureSurveyContent;
import com.zhuoyue.elmt.service.FailureReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.FailureReplacementPart;
import com.zhuoyue.elmt.service.FailureReplacementPartService;

import java.util.ArrayList;
import java.util.List;

/**
 * 电梯配件更换记录Controller
 *
 * @author chenhao
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/failureReplacementPart")
public class FailureReplacementPartController extends BaseController {

    @Autowired
    private FailureReplacementPartService failureReplacementPartService;

    @Autowired
    private FailureReportService failureReportService;

    @ModelAttribute
    public FailureReplacementPart get(@RequestParam(required = false) String id) {
        FailureReplacementPart entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = failureReplacementPartService.get(id);
        }
        if (entity == null) {
            entity = new FailureReplacementPart();
        }
        return entity;
    }

    @RequiresPermissions("elmt:failureReplacementPart:view")
    @RequestMapping(value = {"list", ""})
    public String list(FailureReplacementPart failureReplacementPart, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<FailureReplacementPart> page = failureReplacementPartService.findPage(new Page<FailureReplacementPart>(request, response), failureReplacementPart);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/failureReplacementPartList";
    }

    @RequiresPermissions("elmt:failureReplacementPart:view")
    @RequestMapping(value = "form")
    public String form(FailureReplacementPart failureReplacementPart, Model model) {
        model.addAttribute("failureReplacementPart", failureReplacementPart);
        return "zhuoyue/elmt/failureReplacementPartForm";
    }

    @RequiresPermissions("elmt:failureReplacementPart:edit")
    @RequestMapping(value = "save")
    public String save(FailureReplacementPart failureReplacementPart, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, failureReplacementPart)) {
            return form(failureReplacementPart, model);
        }
        failureReplacementPartService.save(failureReplacementPart);
        addMessage(redirectAttributes, "保存配件更换记录成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/failureReplacementPart/?repage";
    }

    @RequiresPermissions("elmt:failureReplacementPart:edit")
    @RequestMapping(value = "delete")
    public String delete(FailureReplacementPart failureReplacementPart, RedirectAttributes redirectAttributes) {
        failureReplacementPartService.delete(failureReplacementPart);
        addMessage(redirectAttributes, "删除配件更换记录成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/failureReplacementPart/?repage";
    }

    /**
     * 查询单条配件更换记录
     *
     * @param str1
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getFailureReplacementPartInfo/{str1}")
    public String getFailureReplacementPartInfo(@PathVariable String str1, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        List<FailureReplacementPart> list = new ArrayList();
        try {
            System.out.println("查询单条配件更换记录 ===========================================>>>>>>>>>>>>>>");
            FailureReplacementPart record = failureReplacementPartService.get(str1);
            if (record != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件的电梯配件更换记录！");
            }
            list.add(record);
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯配件更换记录查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    /**
     * 查询多条配件更换记录
     *
     * @param failureReplacementPart
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findFailureReplacementPartInfo")
    public String findFailureReplacementPartInfo(FailureReplacementPart failureReplacementPart, HttpServletRequest request, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        try {
            System.out.println("查询多条配件更换记录 ===========================================>>>>>>>>>>>>>>");
            Page<FailureReplacementPart> page = failureReplacementPartService.findPage(new Page<FailureReplacementPart>(request, response), failureReplacementPart);
            if (null != page) {
                resultData.setList(page.getList());
            }
            resultData.setCode("SUCC");
            resultData.setMessage("");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯配件更换记录查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveFailureReplacementPartInfo")
    public String saveFailureReplacementPartInfo(FailureReplacementPart failureReplacementPart, Model model, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        List<FailureReplacementPart> list = new ArrayList();
        try {
            System.out.println("增加电梯配件更换记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureReplacementPart)) {
                return form(failureReplacementPart, model);
            }
            failureReplacementPartService.save(failureReplacementPart);
            System.out.println("wbFailureReport.getRecordId() ================>>>>>>>>>>>>>> " + failureReplacementPart.getFormId());

            //增加返回model
            list.add(failureReplacementPart);
            resultData.setList(list);

            resultData.setFormId(failureReplacementPart.getFormId());
            resultData.setCode("SUCC");
            resultData.setMessage("增加电梯配件更换记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("增加电梯配件更换记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/updateFailureReplacementPartInfo")
    public String updateFailureReplacementPartInfo(FailureReplacementPart failureReplacementPart, Model model, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        List<FailureReplacementPart> list = new ArrayList();

        try {
            System.out.println("更新电梯配件更换记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureReplacementPart)) {
                return form(failureReplacementPart, model);
            }
            if (StringUtils.isNotEmpty(failureReplacementPart.getFormId())) {
                failureReplacementPartService.update(failureReplacementPart);

                //增加返回model
                list.add(failureReplacementPart);
                resultData.setList(list);

                resultData.setCode("SUCC");
                resultData.setMessage("更新电梯配件更换记录成功");
            }
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("更新电梯配件更换记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deleteFailureReplacementPartInfo")
    public String deleteFailureReplacementPartInfo(FailureReplacementPart failureReplacementPart, Model model, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        List<FailureReplacementPart> list = new ArrayList();
        try {
            System.out.println("删除电梯配件更换记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureReplacementPart)) {
                return form(failureReplacementPart, model);
            }
            failureReplacementPartService.delete(failureReplacementPart);

            resultData.setFormId(failureReplacementPart.getFormId());
            resultData.setCode("SUCC");
            resultData.setMessage("删除电梯配件更换记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("删除电梯配件更换记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/savePart")
    public String savePart(FailureReport failureReport, String[] formId, String[] accessoriesName, String[] accessoriesModel, String[] accessoriesNumber, String[] drawingNumber, String deviceCode, String failureNo, HttpServletResponse response) {
        ResultData<List<FailureReplacementPart>> resultData = new ResultData();
        List<FailureReplacementPart> list = new ArrayList();
        try {
            System.out.println("电梯配件记录 ===========================================>>>>>>>>>>>>>>");
            FailureReport qryFailureReport = new FailureReport();
            qryFailureReport.setFailureNo(failureNo);
            FailureReport newFailureReport = failureReportService.getByFailureNo(qryFailureReport);
            // 更新 状态
            newFailureReport.setStatus(failureReport.getStatus());

            if (formId != null && formId.length > 0) {
                // 如果只有一条数据
                if (formId.length == 1) {
                    for (int i = 0; i < formId.length; i++) {
                        FailureReplacementPart failureReplacementPart = new FailureReplacementPart();
                        if (!"0".equals(formId[i])) {
                            failureReplacementPart.setFormId(formId[i]);
                        }
                        failureReplacementPart.setDeviceCode(deviceCode);
                        failureReplacementPart.setFailureNo(failureNo);
                        if (StringUtils.isNotEmpty(accessoriesName[i])) {
                            failureReplacementPart.setAccessoriesName(accessoriesName[i]);
                        }
                        if (StringUtils.isNotEmpty(accessoriesModel[i])) {
                            failureReplacementPart.setAccessoriesModel(accessoriesModel[i]);
                        }
                        if (StringUtils.isNotEmpty(accessoriesNumber[i])) {
                            failureReplacementPart.setAccessoriesNumber(accessoriesNumber[i]);
                        }
                        if (StringUtils.isNotEmpty(drawingNumber[i])) {
                            failureReplacementPart.setDrawingNumber(drawingNumber[i]);
                        }

                        //增加返回model
                        list.add(failureReplacementPart);
                    }
                } else {
                    for (int i = 0; i < formId.length; i++) {
                        FailureReplacementPart failureReplacementPart = new FailureReplacementPart();
                        if (!"0".equals(formId[i])) {
                            failureReplacementPart.setFormId(formId[i]);
                        }
                        failureReplacementPart.setDeviceCode(deviceCode);
                        failureReplacementPart.setFailureNo(failureNo);
                        failureReplacementPart.setAccessoriesName(accessoriesName[i]);
                        failureReplacementPart.setAccessoriesModel(accessoriesModel[i]);
                        failureReplacementPart.setAccessoriesNumber(accessoriesNumber[i]);
                        failureReplacementPart.setDrawingNumber(drawingNumber[i]);

                        //增加返回model
                        list.add(failureReplacementPart);
                    }
                }
            }

            failureReplacementPartService.saveOrUpdate(newFailureReport, list);

            resultData.setList(list);

            resultData.setCode("SUCC");
            resultData.setMessage("电梯配件记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯配件出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }
}